﻿using System;
using System.Collections.Generic;
using System.Text;
using Lon.Data;
using Lon.Util;
using Lon.IO;
using System.IO;

namespace Lon.Server
{
    public class NetRawDataRec : RawDataRecBase
    {
        static public String[] dataTypeNames = new String[256];
        static public Dictionary<String, byte> dataTypeNameDict = new Dictionary<string, byte>();

        public String SrcDbm;
        public String DstDbm;
        public String FrameType;

        public String MajorDataTypeText;

        public MajorDataType MajorDataType;

        public int MinorDataType;

        static NetRawDataRec()
        {
            for (int i = 0; i < 255; i++)
            {
                dataTypeNames[i] = i.ToString("X2");
            }
            PathHelper.GetApplicationPath();
            IniFile cfg = new IniFile(Path.Combine(PathHelper.GetApplicationPath(), "类型名称.ini"));
            cfg.Load();
            for (int i = 0; i < 255; i++)
            {
                String dataTypeName = cfg.GetString("数据类型", i.ToString());
                if (String.IsNullOrEmpty(dataTypeName)) continue;
                dataTypeNameDict[dataTypeName] = (byte)i;
                dataTypeNames[i] = dataTypeName;
            }
        }
        public NetRawDataRec(DataFrame df, Lon.Data.DataDir dir)
        {
            base.Time = DateTime.Now;
            this.DataDir = dir;
            if (df.Buf.Length < 100)
            {
                this.Data = df.Buf;
            }
            else
            {
                this.Data = new byte[100];
                Array.Copy(df.Buf, this.Data, this.Data.Length);

            }
            this.SrcDbm=df.SrcDbm;
            this.DstDbm = df.DstDbm;
            if(df.IsCommand)
            {
                this.FrameType="命令帧";
            }
            else
            {
                this.FrameType="命令帧";
            }
            this.MajorDataType=df.MajorDataType;
            this.MajorDataTypeText=dataTypeNames[(int)this.MajorDataType];
            this.MinorDataType=df.MinorDataType;
               
            



        }
        private String dataStr;
        public override string DataStr
        {
            get
            {
                if (!String.IsNullOrEmpty(dataStr)) return dataStr;
                if (base.Data == null) return "";
                StringBuilder sb = new StringBuilder(base.Data.Length * 3 + 200);
                if (base.Data[4] == 0x0f)
                {
                    this.dataStr = "心跳帧";
                    return this.dataStr;
                }
                if (base.Data.Length < 16)
                {
                    sb.Append("非法数据帧:");
                    sb.Append(StringHelper.ByteArr2HexString(base.Data));
                    this.dataStr = sb.ToString();
                    return this.dataStr;
                }
                sb.Append("[数据长度]");
                sb.Append(StringHelper.ByteArr2HexString(base.Data, 0, 4, " "));
                sb.Append("  [标识]");
                sb.Append(base.Data[4].ToString("X2"));
                sb.Append("  [目的电报码]");
                sb.Append(StringHelper.ByteArr2HexString(base.Data, 5, 3, " "));
                sb.Append("  [源电报码]");
                sb.Append(StringHelper.ByteArr2HexString(base.Data, 8, 3, " "));
                sb.Append("  [数据/命令代码]");
                sb.Append(base.Data[11].ToString("X2"));
                if (base.Data[11] == 0x00)
                {
                    sb.Append("  [压缩标示]");
                    sb.Append(base.Data[12].ToString("X2"));
                    sb.Append("  [版本码]");
                    sb.Append(base.Data[13].ToString("X2"));
                    sb.Append("  [数据类型码]");
                    sb.Append(base.Data[14].ToString("X2"));
                    sb.Append("  [数据子类型码]");
                    sb.Append(base.Data[15].ToString("X2"));
                    sb.Append("  [数据]");
                    sb.Append(StringHelper.ByteArr2HexString(base.Data, 16, base.Data.Length - 16, " "));
                }
                else
                {
                    sb.Append("  [版本码]");
                    sb.Append(base.Data[12].ToString("X2"));
                    sb.Append("  [数据类型码]");
                    sb.Append(base.Data[13].ToString("X2"));
                    sb.Append("  [数据子类型码]");
                    sb.Append(base.Data[14].ToString("X2"));
                    sb.Append("  [数据]");
                    sb.Append(StringHelper.ByteArr2HexString(base.Data, 15, base.Data.Length - 15, " "));
                }
                this.dataStr = sb.ToString();
                return dataStr;
            }
        }
    }

    public class RawDataUpdateEventArgs : EventArgs
    {
        public NetRawDataRec Rec { get; private set; }
        public RawDataUpdateEventArgs(NetRawDataRec rec)
        {
            this.Rec = rec;
        }
    }
}
